import { IModalData, IOpenViewUtil, IViewConfig } from '@ibiz-template/runtime';
import { generateRoutePath } from '@ibiz-template/vue3-util';
import { Router } from 'vue-router';
import {
  getDrawerPlacement,
  openViewDrawer,
  openViewModal,
  openViewPopover,
} from '../overlay-view-util/overlay-view-util';
import { routerCallback } from '../router-callback/router-callback';

/**
 * 打开视图方式工具类
 *
 * @description 此实现类挂载在 ibiz.openViewUtil
 * @author chitanda
 * @date 2022-08-16 20:08:54
 * @export
 * @class OpenViewUtil
 * @implements {IOpenViewUtil}
 */
export class OpenViewUtil implements IOpenViewUtil {
  // eslint-disable-next-line no-useless-constructor, no-empty-function
  constructor(protected router: Router) {}

  async root(
    appView: IViewConfig,
    context: IContext,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    const { path } = await generateRoutePath(
      appView,
      this.router.currentRoute.value,
      context,
      params,
    );
    return routerCallback.open(this.router, path);
  }

  /**
   * 模态打开视图
   *
   * @author lxm
   * @date 2022-09-12 01:09:06
   * @param {IViewConfig} appView
   * @param {(IContext | undefined)} [context]
   * @param {(IParams | undefined)} [params]
   * @returns {*}  {Promise<IModalData>}
   */
  async modal(
    appView: IViewConfig,
    context: IContext,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    // 设置默认的modal参数
    const opts = {
      width: appView.width || '80%',
      height: appView.height || '80%',
      footerHide: true,
    };

    return openViewModal(
      {
        context,
        params,
        viewId: appView.id,
      },
      opts,
    );
  }

  async popover(
    appView: IViewConfig,
    event: MouseEvent,
    context: IContext,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    return openViewPopover(
      event,
      {
        context,
        params,
        viewId: appView.id,
      },
      { autoClose: true, placement: 'bottom' },
    );
  }

  /**
   * 抽屉打开视图
   *
   * @author lxm
   * @date 2022-09-15 15:09:50
   * @param {IViewConfig} appView
   * @param {(IContext | undefined)} [context]
   * @param {(IParams | undefined)} [params]
   * @returns {*}  {Promise<IModalData>}
   */
  async drawer(
    appView: IViewConfig,
    context: IContext,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    const placement = getDrawerPlacement(appView.openMode!);

    // 设置默认的modal参数
    const opts = {
      width: appView.width || '800',
      height: appView.height || '600',
      placement,
    };

    return openViewDrawer(
      {
        context,
        params,
        viewId: appView.id,
      },
      opts,
    );
  }

  async custom(
    appView: IViewConfig,
    context: IContext,
    params?: IParams | undefined,
  ): Promise<IModalData> {
    ibiz.log.warn('openUserCustom', appView, context, params);
    throw new Error();
  }
}
